import AttributeUtil from '@/AttributeUtil';
import BuffUtil from '@/BuffUtil';

declare global {
    interface AddBuffConfig {
        target: unit;
        buffTypeId: string;
        ablityId: number | string;
        caster?: unit;
        attribute?: AppAttribute;
        config?: Partial<AppBuffType> & { stack?: number };
    }
}

export default class BuffCommon {
    static addBuff(cfg: AddBuffConfig) {
        let { target, buffTypeId, caster, attribute, config, ablityId } = cfg;
        let buff = BuffUtil.addBuff(target, buffTypeId, caster);
        let dur = 0;
        let stack = 0;

        // 处理额外添加的层数与时间
        if (caster != null) {
            let attr = AttributeUtil.getUnitAttribute(caster);
            if (attr != null) {
                dur += (attr?.[ablityId + '_effect_dur'] as number) ?? 0;
                stack += (attr?.[ablityId + '_effect_count'] as number) ?? 0;
            }
        }
        if (config != null) {
            dur += config.dur ?? 0;
            stack += config.stack ?? 0;
        }
        if (dur > 0) {
            buff.durMS += dur * 1000;
        }
        if (stack > 0) {
            buff.stackCount = stack;
        }

        // 处理额外添加的属性
        if (attribute != null) {
            if (buff.attribute == null) {
                buff.attribute = {};
            }
            AttributeUtil.add(buff.attribute, attribute);
        }

        return buff;
    }
}
